/**
 * Created by ajhin on 1/29/2015.
 */
package com.societyGames.socialFeedExercises
{
import com.societyGames.action.ActionQueue;
import com.societyGames.flashForms.CenterScaleContainer;
import com.societyGames.flashForms.DynamicTextField;

import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;

public class SocialFeedRender extends Sprite
{
  [Embed(source="../../../../assets/App.png")]
  private static var CatImageClass:Class;

  [Embed(source="../../../../assets/Social.png")]
  private static var FaceImageClass:Class;

  private static const DISPLAY_WIDTH:int = 250; //TODO: Move this to shared location.
  private static const DISPLAY_HEIGHT:int = 250;
  private static const LEFT_GUTTER_SIZE:int = 5;
  private static const RIGHT_GUTTER_SIZE:int = 5;
  private static const TOP_GUTTER_SIZE:int = 5;
  private static const BOTTOM_GUTTER_SIZE:int = 84;

  private var _simulation:SocialFeedSimulation;
  private var _actionQueue:ActionQueue;

  private var _applicationTextField:TextField;
  private var _socialTextField:TextField;

  public function SocialFeedRender(simulation:SocialFeedSimulation, actionQueue:ActionQueue)
  {
    _simulation = simulation;
    _actionQueue = actionQueue;

    var applicationBackground:DisplayObject = DisplayObject(new CatImageClass());
    addChild(applicationBackground);

    var socialBackground:DisplayObject = DisplayObject(new FaceImageClass());
    addChild(socialBackground);
    socialBackground.x = applicationBackground.width;

    this._applicationTextField = createTextField();
    this._applicationTextField.width = (DISPLAY_WIDTH / 2) - LEFT_GUTTER_SIZE - RIGHT_GUTTER_SIZE;
    this._applicationTextField.height = DISPLAY_HEIGHT - TOP_GUTTER_SIZE - BOTTOM_GUTTER_SIZE;
    var applicationTextCenterContainer:CenterScaleContainer = new CenterScaleContainer();
    applicationTextCenterContainer.item = this._applicationTextField;
    applicationTextCenterContainer.x = LEFT_GUTTER_SIZE;
    applicationTextCenterContainer.y = TOP_GUTTER_SIZE;
    applicationTextCenterContainer.width = (DISPLAY_WIDTH / 2) - LEFT_GUTTER_SIZE - RIGHT_GUTTER_SIZE;
    applicationTextCenterContainer.height = DISPLAY_HEIGHT - TOP_GUTTER_SIZE - BOTTOM_GUTTER_SIZE;
    this.addChild(applicationTextCenterContainer);

    this._socialTextField = createTextField();
    this._socialTextField.width = (DISPLAY_WIDTH / 2) - LEFT_GUTTER_SIZE - RIGHT_GUTTER_SIZE;
    this._socialTextField.height = DISPLAY_HEIGHT - TOP_GUTTER_SIZE - BOTTOM_GUTTER_SIZE;
    var socialTextCenterContainer:CenterScaleContainer = new CenterScaleContainer();
    socialTextCenterContainer.item = this._socialTextField;
    socialTextCenterContainer.x = (DISPLAY_WIDTH / 2) + LEFT_GUTTER_SIZE;
    socialTextCenterContainer.y = TOP_GUTTER_SIZE;
    socialTextCenterContainer.width = (DISPLAY_WIDTH / 2) - LEFT_GUTTER_SIZE - RIGHT_GUTTER_SIZE;
    socialTextCenterContainer.height = DISPLAY_HEIGHT - TOP_GUTTER_SIZE - BOTTOM_GUTTER_SIZE;
    this.addChild(socialTextCenterContainer);
  }

  private function createTextField():DynamicTextField
  {
    var textField:DynamicTextField = new DynamicTextField();
    textField.autoSize = TextFieldAutoSize.CENTER;
    textField.wordWrap = true;
    textField.multiline = true;

    var textFormat:TextFormat = new TextFormat();
    textFormat.align = TextFormatAlign.CENTER;
    textFormat.size = 16;
    textField.defaultTextFormat = textFormat;

    return textField;
  }

  public function postToFeed():void
  {
    this._actionQueue.add(new ShowTextAction(this._applicationTextField, _simulation.applicationText, 1000));

    //The simulation can't know the success or failure until this UI action completes.
    var socialFeedAction:SocialFeedAction = new SocialFeedAction();
    socialFeedAction.beforeCompleteEventObject.addEventListener(onSocialFeedActionComplete, false, 0, true);
    this._actionQueue.add(socialFeedAction);
  }

  private function onSocialFeedActionComplete(event:Event):void
  {
    //When the UI action is complete, tell the simulation what happened.
    var socialFeedAction:SocialFeedAction = event.target as SocialFeedAction;
    socialFeedAction.beforeCompleteEventObject.removeEventListener(onSocialFeedActionComplete);

    var message:String = socialFeedAction.responseMessage;
    if (socialFeedAction.responseSuccess)
    {
      message = "Success!";
    }
    else
    {
      if (message == null)
      {
        message = "Unknown error.";
      }
      message = "Oops!\n" + message;
    }
    _actionQueue.add(new ShowTextAction(this._socialTextField, message, 500));
    _simulation.handlePostToFeedComplete(socialFeedAction.responseSuccess);
  }
}
}
